package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.core.JWTUtil;
import com.tjnu.losca.core.ResponseVO;
import com.tjnu.losca.mapper.TeacherMapper;
import com.tjnu.losca.pojo.Teacher;
import com.tjnu.losca.service.ITeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author losca
 * @since 2021-09-16
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //登陆处理
    @Override
    public ResponseVO<String> login(String username, String password) throws Exception {
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            //按用户名查询
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("name",username).or()
                    .eq("phone",username).or()
                    .eq("email",username);
            Teacher teacher = this.getOne(wrapper);
            //如果一条都没有用户不存在把异常往上抛给统一异常处理
            if (teacher == null || !passwordEncoder.matches(password,teacher.getPassword())){
                throw new Exception("用户名或密码错误");
            }
            return ResponseVO.success("登陆成功", JWTUtil.create(teacher));
        }
        return ResponseVO.failed("用户名或者密码不能为空");


    }

    @Override
    public boolean recovery(Integer id) {
        Teacher teacher = this.getById(id);
        //状态恢复成0表示正常
        teacher.setStatus(0);
        return this.updateById(teacher);
    }

    /**
     * 对手机号 邮箱进行验证
     * @param value
     * @return
     */
    @Override
    public boolean check(Integer id,String value) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",value);
        if (id != null){
            wrapper.ne("id",id);
        }
        Teacher teacher = this.getOne(wrapper);
        //查询到老师说明手机号重复，返回false
        if (teacher != null){
            return false;
        }
        return true;
    }

    @Override
    public IPage getTeacherBySearch(int pageNum, int pageSize, String search) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(search)){
        wrapper.like("name",search).or()
                .like("email",search).or()
                .like("phone",search).or()
                .like("id",search);
        }
        return this.page(new Page<>(pageNum,pageSize),wrapper);
    }

    @Override
    public boolean del(Integer id) {
        Teacher teacher = this.getById(id);
        teacher.setStatus(1);
        return this.updateById(teacher);
    }
}
